package com.lwc.backendsinglestructure.llm.embedding;

import com.lwc.backendsinglestructure.exception.BusinessException;
import com.lwc.backendsinglestructure.llm.enums.EmbeddingModelEnum;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 抽象大模型对话工厂服务
 */
@Component
public class EmbeddingModelFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    private static final Map<String, AbstractEmbeddingModel> serviceMap = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.initServiceMap();
    }

    /**
     * 初始化模型服务
     */
    private void initServiceMap() {
        /**
         * aliabba
         */
        serviceMap.put(EmbeddingModelEnum.ALIBABA.getModel(), applicationContext.getBean(AlibabaEmbeddingModel.class));
    }

    public static AbstractEmbeddingModel getEmbeddingModel(String model) {
        AbstractEmbeddingModel llmChatService = serviceMap.get(model);
        if (llmChatService == null) {
            throw new RuntimeException("不支持的模型");
        }
        return llmChatService;
    }
}
